<?php

namespace application\controllers;

use application\BaseController;
use fastphp\facade\Db;
use fastphp\facade\Request;

class fileName extends BaseController
{
    public function index()
    {
        $param = Request::param();
        if (Request::isGet()) {
            $this->_view->render();
        } else {
            if (!empty($param)) {
                if (!empty($param['id'])) {
                    $res = Db::table('xxx')
                        ->where('id', $param['id'])
                        ->find();
                } else {
                    $res = Db::table('xxx')
                        ->page($param['page'] ?? 1,
                            $param['limit'] ?? 10)
                        ->select();
                }
            } else {
                $res = Db::table('xxx')
                    ->select();
            }
            $this->json('', $res, 0, ['count' => Db::table('xxx')->getTotalRecords()]);
        }
    }

    public function add()
    {
        $param = Request::param();

        if (Request::isGet()) {
            $this->_view->render();
        } else {
            if (empty($param)) {
                $this->json('参数不能为空',null,400);
                exit();
            }
            $data = [];
            $sql = "SHOW COLUMNS FROM tableName";
            $db = new Db();
            $table = $db->sql_query($sql);
            foreach ($table as $item) {
                if ($item['Key'] != 'PRI') {
                    $data[$item['Field']] = $param[$item['Field']];
                }
            }
            $res = Db::table('xxx')
                ->insert($data);
            if (empty($res)) {
                $this->json('操作失败', null, 400);
                exit();
            }
            $this->json('操作成功');
        }
    }

    public function update()
    {
        $param = Request::param();
        if (Request::isGet()) {
            if (!empty($param)) {
                $res = Db::table('xxx')
                    ->where('id', $param['id'])
                    ->find();
                if (!empty($res)) {
                    $this->_view->set('info', $res);
                }
                $this->_view->render();
            }
        } else {
            if (empty($param)) {
                $this->json('参数不能为空',null,400);
                exit();
            }
            $data = [];
            $sql = "SHOW COLUMNS FROM tableName";
            $db = new Db();
            $table = $db->sql_query($sql);
            foreach ($table as $item) {
                if ($item['Key'] != 'PRI') {
                    $data[$item['Field']] = $param[$item['Field']];
                }
            }
            $res = Db::table('xxx')
                ->where('id', $param['id'])
                ->update($data);
            if (empty($res)) {
                $this->json('操作失败', null, 400);
                exit();
            }
            $this->json('操作成功');
        }
    }

    public function delete()
    {
        $id = Request::param('id');
        $ids = Request::param('ids');
        if (empty($id) && empty($ids)) {
            $this->json('缺少id');
            exit();
        }

        if (!empty($id)){
            $res = Db::table('xxx')->where('id', $id)->delete();
        }else{
            $res = Db::table('xxx')->where('id','in',$ids)->delete();
        }
        if (empty($res)) {
            $this->json('删除失败',null,400);
            exit();
        }
        $this->json('删除成功');
    }
}